package com.wngbms;

import com.alibaba.fastjson.JSON;
import com.hrinfoservice.HrInfoservice;
import com.hrinfoservice.HrInfoservicePortType;
import com.wngbms.entity.Hr;
import com.wngbms.entity.Organize;
import com.wngbms.entity.User;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) throws Exception {

//        user();
//        org();
//        decUser();
//        decOrg();

        HrInfoservicePortType hrInfoservicePortType = new HrInfoservice().getHrInfoserviceHttpPort();
        System.out.println(args[0] + "\t" + args[1]);

        String mj = hrInfoservicePortType.getAllUser(args[0], args[1]);
        System.out.println(mj);
        decUser(mj);

        String allOrganizations = hrInfoservicePortType.getAllOrganizations(args[0], args[1]);
        System.out.println(allOrganizations);
        decOrg(allOrganizations);
    }

    static void decOrg(String resp) throws JAXBException, IOException {
        BufferedReader reader = new BufferedReader(new StringReader(resp));

        JAXBContext jaxbContext = JAXBContext.newInstance(Hr.class, Organize.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Hr org = (Hr) unmarshaller.unmarshal(reader);

        System.out.println(org.toString());
        System.out.println(JSON.toJSONString(org));

        reader.close();
    }

    static void decOrg() throws JAXBException {
        File file = new File("org.xml");

        JAXBContext jaxbContext = JAXBContext.newInstance(Hr.class, Organize.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Hr organize = (Hr) unmarshaller.unmarshal(file);

        System.out.println(organize.toString());
        System.out.println(JSON.toJSONString(organize));
    }

    static void decUser(String resp) throws JAXBException, IOException {
        BufferedReader reader = new BufferedReader(new StringReader(resp));

        JAXBContext jaxbContext = JAXBContext.newInstance(Hr.class, User.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Hr user = (Hr) unmarshaller.unmarshal(reader);

        System.out.println(user.toString());
        System.out.println(JSON.toJSONString(user));

        reader.close();
    }

    static void decUser() throws JAXBException {
        File file = new File("hr.xml");

        JAXBContext jaxbContext = JAXBContext.newInstance(Hr.class, User.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Hr hr = (Hr) unmarshaller.unmarshal(file);

        System.out.println(hr.toString());
        System.out.println(JSON.toJSONString(hr));
    }

    static void org() throws JAXBException, IOException {
        List<Organize> organizes = new ArrayList<>();
        Organize organize = new Organize("123", "123", "123", "123", "123", "123", "123", "123", "123", "123");
        organizes.add(organize);
        organize = new Organize("123", "123", "123", "123", "123", "123", "123", "123", "123", "123");
        organizes.add(organize);
        organize = new Organize("123", "123", "123", "123", "123", "123", "123", "123", "123", "123");
        organizes.add(organize);

        Hr<Organize> hr = new Hr("人力资源系统", "zh-cn", organizes);
        hr.setVersion("5.0");

        JAXBContext context = JAXBContext.newInstance(Hr.class, Organize.class);//使用对象生成xml
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//                marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");//放开就是GBK格式
        FileWriter fw = null;
        try {
            fw = new FileWriter("org.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null != hr) {
            marshaller.marshal(hr, fw);
        }
        fw.flush();
        fw.close();
    }

    private static void user() throws JAXBException, IOException {
        List<User> users = new ArrayList<>();
        User user = new User("1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234");
        user.setFlag("biandong");
        users.add(user);
        user = new User("1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234");
        user.setFlag("biandong");
        users.add(user);
        user = new User("1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234", "1234");
        user.setFlag("biandong");
        users.add(user);

        Hr<User> hr = new Hr("人力资源系统", "zh-cn", users);
        hr.setVersion("5.0");

        JAXBContext context = JAXBContext.newInstance(Hr.class, User.class);//使用对象生成xml
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//                marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");//放开就是GBK格式
        FileWriter fw = null;
        try {
            fw = new FileWriter("hr.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null != hr) {
            marshaller.marshal(hr, fw);
        }
        fw.flush();
        fw.close();
    }


}
